﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Smatime.Web.DAO;
using Smatime.Web.Models;

namespace Smatime.Web.Controllers
{
    public class UserAccessController : ApiController
    {
        // GET api/useraccess
        public IEnumerable<string> Get()
        {
            return new[] { "value1", "value2" };
        }

        // POST api/useraccess
        public HttpResponseMessage Post([FromBody] UserAccess value)
        {
            try
            {
                var repos = new UserAccessRepository();
                var useraccess = repos.GetByEmail(value.Email);
                if (useraccess == null)
                {
                    value.RowStatus = 0;
                    repos.Insert(value);
                    useraccess = repos.GetByEmail(value.Email);
                    return useraccess == null
                        ? Request.CreateResponse(HttpStatusCode.BadRequest)
                        : Request.CreateResponse(HttpStatusCode.OK, useraccess.Id);
                }
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // PUT api/useraccess/5
        public void Put(int id, [FromBody] UserAccess value)
        {
            var repos = new UserAccessRepository();
            value.Id = id;
            repos.Update(value);
        }

        // DELETE api/useraccess/5
        public void Delete(int id)
        {
        }
    }
}